package persistent.impl;

import java.io.Serializable;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import persistent.dao.CloudUserDAO;
import util.toolkit.Constant;
import util.toolkit.LogUtil;
import basic.HibBaseDAO;
import basic.HibernateSessionFactory;
import model.TCloudUser;
import model.TParentInfo;
import model.TTeacherInfo;

/**
 * 用户业务接口的实现类
 * @author 徐震霖
 * @since 2018-12-06
 */
public class CloudUserImpl implements CloudUserDAO {

	private HibBaseDAO dao = null;
	private static final Log log = LogFactory.getLog(ParentInfoImpl.class);
	
	public CloudUserImpl() {
		dao = new HibBaseDAO();
	}
	
	/**
	 * 根据用户id得到一个用户对象
	 * @param userid 用户id
	 * @return 成功返回TCloudUser对象，失败返回null
	 */
	@Override
	public TCloudUser getTCloudUser(int userid) {
		return (TCloudUser)dao.findById(TCloudUser.class, userid);
	}
	
	/**
	 * 用户密码修改
	 * @param clouduser 用户对象
	 * @return 成功返回true，失败返回false
	 */
	@Override
	public boolean updateCloudUser(TCloudUser clouduser) {
		TCloudUser clouduser2=(TCloudUser)dao.findById(TCloudUser.class, clouduser.getUserid());
		clouduser2.setUserName(clouduser.getUserName());
		clouduser2.setPwd(clouduser.getPwd());
		clouduser2.setPhone(clouduser.getPhone());
		clouduser2.setRoleid(clouduser.getRoleid());
		clouduser2.setStatus(clouduser.getStatus());
		return dao.update(clouduser2);
	}

	/**
	 * 根据登录的用户，到教师表中查询得到一个教师对象
	 * @param phone 登录用户电话
	 * @return 成功返回TTeacherInfo对象，失败返回false
	 */
	@Override
	public List<TTeacherInfo> getTeacherByUserid(String phone) {
		String hql="from TTeacherInfo where phone=?";
		Object para[]={phone};
		return dao.select(hql, para);
	}
	
	/**
	 * 判断用户手机号在数据库中是否存在
	 * @param phone 手机号
	 * @param userType 用户类型（教师或家长）
	 * @return 存在返回false，不存在返回true
	 */
	@Override
	public boolean whetherUserName(String phone, String userType) {
		if (userType.equals("0")) {
			String hql = "from TTeacherInfo where phone = ?";
			Object[] para = { phone };
			List<TTeacherInfo> tealist = dao.select(hql, para);
			if (tealist != null && tealist.size() > 0) {
				return false;
			}
			else {
				return true;
			}
		}
		else {
			String hql = "from TParentInfo where phone = ?";
			Object[] para = { phone };
			List<TParentInfo> parlist = dao.select(hql, para);
			if (parlist != null && parlist.size() > 0) {
				return false;
			}
			else {
				return true;
			}
		}
	}

	/**
	 * 判断当前用户手机号是否已被激活
	 * @param phone 用户手机号码
	 * @return 存在返回false，不存在返回true
	 */ 
	@Override
	public boolean whetherPhone(String phone) {
		String hql = "from TCloudUser where phone = ?";
		Object[] para = { phone };
		List<TCloudUser> parlist = dao.select(hql, para);
		if (parlist != null && parlist.size() > 0) {
			return false;
		}
		else {
			return true;
		}
	}

}
